﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:System.Reactive.Contrib.Monitoring.UI"
                    xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options">
    <local:MarbleTemplateSelector x:Key="_marbleTemplateSelector" />
    <local:MarbleDiagramTemplateSelector x:Key="_marbleDiagramTemplateSelector" />

    <local:ImageCoverter x:Key="imgConv" />
    <DrawingBrush x:Key="gridBrush"
                  po:Freeze="True"
                  TileMode="Tile"
                  Viewport="0,0,46,46"
                  ViewportUnits="Absolute">
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <DrawingGroup.Children>
                    <GeometryDrawing Geometry="M0,0 L46,0">
                        <GeometryDrawing.Pen>
                            <Pen Brush="Black" Thickness="0.5" />
                        </GeometryDrawing.Pen>
                    </GeometryDrawing>
                    <GeometryDrawing Geometry="M0,0 L0,46">
                        <GeometryDrawing.Pen>
                            <Pen Brush="Black" Thickness="0.5" />
                        </GeometryDrawing.Pen>
                    </GeometryDrawing>
                </DrawingGroup.Children>
            </DrawingGroup>
        </DrawingBrush.Drawing>
    </DrawingBrush>

    <DataTemplate x:Key="MarbleNextTemplate">
        <Ellipse Width="{Binding DiagramContext.Size}"
                 MinHeight="{Binding DiagramContext.Size}"
                 Fill="{Binding Color}"
                 po:Freeze="True"
                 Stroke="Black"
                 StrokeThickness="2">
            <!--
                <Ellipse.CacheMode>
                <BitmapCache EnableClearType="False"
                RenderAtScale="2"
                SnapsToDevicePixels="False" />
                </Ellipse.CacheMode>
            -->
            <Ellipse.ToolTip>
                <StackPanel>
                    <TextBlock Text="OnNext" />
                    <TextBlock Text="{Binding Item.ThreadId, StringFormat='Thread Id:{0}, Mode=OneTime'}" />
                    <TextBlock Text="{Binding Item.MachineName, StringFormat='Machine:{0}', Mode=OneTime}" />
                    <TextBlock FontWeight="Heavy"
                               Foreground="Black"
                               Text="{Binding Item.Value,
                                              Mode=OneTime}" />
                </StackPanel>
            </Ellipse.ToolTip>
        </Ellipse>
    </DataTemplate>

    <DataTemplate x:Key="MarbleErrorTemplate">
        <Grid Width="{Binding DiagramContext.Size}"
              MinHeight="{Binding DiagramContext.Size}"
              po:Freeze="True">
            <Rectangle Width="7.58"
                       Margin="0,0.054,10.887,-0.046"
                       HorizontalAlignment="Right"
                       Fill="Black"
                       po:Freeze="True"
                       RenderTransformOrigin="0.5,0.5"
                       Stroke="Black">
                <Rectangle.RenderTransform>
                    <TransformGroup>
                        <RotateTransform Angle="45.268" />
                    </TransformGroup>
                </Rectangle.RenderTransform>
            </Rectangle>
            <Rectangle Width="6.699"
                       Margin="11.876,0.373,0,-0.725"
                       HorizontalAlignment="Left"
                       CacheMode="BitmapCache"
                       Fill="Black"
                       po:Freeze="True"
                       RenderTransformOrigin="0.5,0.5"
                       Stroke="Black">
                <Rectangle.RenderTransform>
                    <TransformGroup>
                        <RotateTransform Angle="135.274" />
                    </TransformGroup>
                </Rectangle.RenderTransform>
            </Rectangle>
            <Grid.ToolTip>
                <StackPanel>
                    <StackPanel>
                        <TextBlock Text="OnError" />
                        <TextBlock Text="{Binding Item.ThreadId, StringFormat='Thread Id:{0}', Mode=OneTime}" />
                        <TextBlock Text="{Binding Item.MachineName, StringFormat='Machine:{0}', Mode=OneTime}" />
                        <TextBlock FontWeight="Heavy"
                                   Foreground="Black"
                                   Text="{Binding Item.Value,
                                                  Mode=OneTime}" />
                    </StackPanel>
                </StackPanel>
            </Grid.ToolTip>
        </Grid>
    </DataTemplate>

    <DataTemplate x:Key="MarbleCompleteTemplate">
        <Rectangle Width="10"
                   MinHeight="{Binding DiagramContext.Size}"
                   Fill="Black"
                   po:Freeze="True">
            <Rectangle.ToolTip>
                <StackPanel>
                    <TextBlock Text="OmComplete" />
                    <TextBlock Text="{Binding Item.ThreadId, StringFormat='Thread Id:{0}'}" />
                    <TextBlock Text="{Binding Item.MachineName, StringFormat='Machine:{0}'}" />
                </StackPanel>
            </Rectangle.ToolTip>
        </Rectangle>
    </DataTemplate>

    <Style x:Key="_listboxStyle" TargetType="ListBox">
        <Style.Resources>
            <!--  Background of selected item when focused  -->
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                             po:Freeze="True"
                             Color="Transparent" />
            <!--  Background of selected item when not focused  -->
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
                             po:Freeze="True"
                             Color="Transparent" />
        </Style.Resources>
        <!--  <Setter Property="BorderThickness" Value="0"/>  -->
    </Style>

    <!--  Single diagram template  -->
    <DataTemplate x:Key="DiagramTemplate">
        <Grid Margin="0,3,0,0">
            <Expander HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="#AA333333"
                      IsExpanded="True" Padding="5,20,5,5">
                <StackPanel Margin="5,10,5,5" Orientation="Horizontal">
                    <StackPanel.LayoutTransform>
                        <ScaleTransform ScaleX="{Binding Path=MainContext.DiagramScale}" ScaleY="{Binding Path=MainContext.DiagramScale}" />
                    </StackPanel.LayoutTransform>
                    <Image Width="30" Height="30" HorizontalAlignment="Left"
                           CacheMode="BitmapCache" po:Freeze="True"
                           Source="{Binding Name, Converter={StaticResource imgConv}}"
                           Stretch="UniformToFill" />
                    <Grid HorizontalAlignment="Left" VerticalAlignment="Center">
                        <Rectangle Width="{Binding DiagramWidth}" Height="2" Fill="Black" />
                        <ListBox MinHeight="{Binding Height}" Background="Transparent" BorderThickness="0"
                                 ItemsSource="{Binding Items, Mode=OneWay}"
                                 ItemTemplateSelector="{StaticResource _marbleTemplateSelector}"
                                 Style="{StaticResource _listboxStyle}">
                            <ListBox.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <Canvas MinHeight="{Binding Size, Mode=OneWay}" Background="Transparent"
                                            IsItemsHost="True" />
                                </ItemsPanelTemplate>
                            </ListBox.ItemsPanel>
                            <ListBox.ItemContainerStyle>
                                <Style TargetType="ListBoxItem">
                                    <Setter Property="Canvas.Left" Value="{Binding Left}" />
                                </Style>
                            </ListBox.ItemContainerStyle>
                        </ListBox>
                    </Grid>
                </StackPanel>
            </Expander>
            <Border Margin="35,14,15,0" HorizontalAlignment="Left" VerticalAlignment="Top"
                    Background="Gray" BorderBrush="Black" BorderThickness="1" CornerRadius="5"
                    Visibility="{Binding ActionBoardVisibility}">
                <StackPanel Orientation="Horizontal" Height="30" Margin="10,2,2,2">
                    <Button Content="Refresh" Command="{Binding RefreshCommand}" FontSize="12"
                                BorderBrush="Black" Background="#AA1D7635" Style="{DynamicResource GlassButtonSmall}" Height="25" MinWidth="80" Margin="1,0,1,0"/>
                    <Button Height="25" MinWidth="80" Margin="10,0,1,0"
                            Background="#AA1D76BE" BorderBrush="Black"
                            Command="{Binding GlobalTimeCommand, Mode=OneTime}"
                            Content="Global Time" FontSize="12"
                            Style="{StaticResource GlassButtonSmall}" />
                    <Button Height="25" MinWidth="80" Margin="10,0,1,0"
                            Background="#AA2492EA" BorderBrush="Black"
                            Command="{Binding PrivateTimeCommand, Mode=OneTime}"
                            Content="Private Time" FontSize="12"
                            Style="{StaticResource GlassButtonSmall}" />
                    <Button Height="25" MinWidth="80" Margin="10,0,1,0"
                            Background="#AA3B3980" BorderBrush="Black"
                            Command="{Binding SequenceCommand, Mode=OneTime}"
                            Content="Sequence" FontSize="12"
                            Style="{StaticResource GlassButtonSmall}" />
                    <Button Margin="10,3,2,0" Cursor="Arrow"
                                Command="{Binding HideActions}"
                                Style="{StaticResource GeometryButton}">
                        <GeometryGroup>
                            <LineGeometry StartPoint="0,0" EndPoint="9,9" />
                            <LineGeometry StartPoint="0,9" EndPoint="9,0" />
                        </GeometryGroup>
                    </Button>
                </StackPanel>
            </Border>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
                <Border Margin="25,0,0,2"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Top"
                        Background="DarkGray"
                        BorderBrush="#FF202020"
                        BorderThickness="1"
                        CornerRadius="4"
                        Padding="2">
                    <Button Margin="2,0,2,2"
                            Command="{Binding ShowActions,
                                              Mode=OneTime}"
                            Content="{Binding Name}"
                            FontSize="10"
                            FontStretch="Expanded"
                            FontWeight="Bold"
                            Foreground="Black"
                            Style="{StaticResource LinkButton}" />
                </Border>
                <TextBlock Margin="10,0,0,0"
                           FontWeight="Bold"
                           Foreground="White"
                           Text="{Binding RawItems.BufferSize}">
                    <TextBlock.Effect>
                        <DropShadowEffect BlurRadius="4"
                                          Direction="330"
                                          Opacity="0.5"
                                          ShadowDepth="2"
                                          Color="Black" />
                    </TextBlock.Effect>
                </TextBlock>
            </StackPanel>
            <TextBlock Margin="10,0,10,0"
                        HorizontalAlignment="Right"
                       VerticalAlignment="Top"
                        FontWeight="Bold"
                        Foreground="White"
                        Text="{Binding RawItems.BufferSize}">
                <TextBlock.Effect>
                    <DropShadowEffect BlurRadius="4"
                                        Direction="330"
                                        Opacity="0.5"
                                        ShadowDepth="2"
                                        Color="Black" />
                </TextBlock.Effect>
            </TextBlock>
        </Grid>
    </DataTemplate>

    <!--  Diagram collection template  -->
    <DataTemplate x:Key="DiagramsTemplate">
        <DockPanel MinWidth="100"
                   MinHeight="100"
                   Margin="5,1,2,2"
                   HorizontalAlignment="Stretch"
                   VerticalAlignment="Stretch">
            <StackPanel Background="Gray" DockPanel.Dock="Top">
                <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
                    <Button Margin="2,0,2,6"
                            Command="{Binding ShowAction}"
                            Content="Show tab actions"
                            FontSize="10"
                            Foreground="Black"
                            Style="{StaticResource LinkButton}" />
                </StackPanel>
                <WrapPanel Margin="0,0,0,4"
                           Orientation="Horizontal"
                           Visibility="{Binding ActionBoardVisibility}">
                    <Button Height="30"
                            MinWidth="80"
                            Margin="10,0,1,0"
                            Background="#FFAB8142"
                            BorderBrush="Black"
                            Command="{Binding ClearCommand}"
                            Content="Clear"
                            FontSize="12"
                            Style="{StaticResource GlassButtonSmall}" />
                    <Button Height="30"
                            MinWidth="80"
                            Margin="10,0,1,0"
                            Background="#AA1D76BE"
                            BorderBrush="Black"
                            Command="{Binding GlobalTimeCommand}"
                            Content="Global Time"
                            FontSize="12"
                            Style="{StaticResource GlassButtonSmall}" />
                    <Button Height="30"
                            MinWidth="80"
                            Margin="10,0,1,0"
                            Background="#AA2492EA"
                            BorderBrush="Black"
                            Command="{Binding PrivateTimeCommand}"
                            Content="Private Time"
                            FontSize="12"
                            Style="{StaticResource GlassButtonSmall}" />
                    <Button Height="30"
                            MinWidth="80"
                            Margin="10,0,1,0"
                            Background="#AA3B3980"
                            BorderBrush="Black"
                            Command="{Binding SequenceCommand}"
                            Content="Sequence"
                            FontSize="12"
                            Style="{StaticResource GlassButtonSmall}" />
                    <Button x:Name="scrollButton"
                            Height="30"
                            MinWidth="80"
                            Margin="10,0,1,0"
                            Background="#AACB990E"
                            BorderBrush="Black"
                            Command="{Binding ScrollAction}"
                            Content="{Binding ScrollText}"
                            FontSize="12"
                            Style="{StaticResource GlassButtonSmall}" />
                    <Slider x:Name="_verticalScale"
                            MinWidth="150" Margin="10,7,0,0"  Orientation="Horizontal"
                            Maximum="1" Minimum="0.4" TickFrequency="0.1"
                            Value="{Binding DiagramScale, Mode=TwoWay}" />
                    <ComboBox MinWidth="100" Margin="5,8,6,5"
                              ItemsSource="{Binding Units, Mode=OneWay}"
                              SelectedItem="{Binding UnitSelected, Mode=TwoWay}" />
                </WrapPanel>
            </StackPanel>
            <ScrollViewer x:Name="scrollViewer"
                          HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
                          local:ScrollViewerExtensions.IsScrollToEnd="{Binding IsScrollToEnd, Mode=TwoWay}">
                <Grid Background="LightSlateGray">
                    <Border Margin="1" BorderBrush="Black" BorderThickness="2,2,1,1">
                        <Border.Effect>
                            <DropShadowEffect BlurRadius="10" Opacity="1" ShadowDepth="0" Color="#8E000000" />
                        </Border.Effect>
                        <ListBox Background="{StaticResource gridBrush}" BorderThickness="0"
                                 ItemsSource="{Binding Diagrams.View}"
                                 ItemTemplateSelector="{StaticResource _marbleDiagramTemplateSelector}"
                                 Style="{StaticResource _listboxStyle}" />
                    </Border>
                </Grid>
            </ScrollViewer>
        </DockPanel>
        <DataTemplate.Triggers>
            <Trigger Property="IsMouseCaptureWithin" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <!--
                                <DoubleAnimation AccelerationRatio="0.6"
                                AutoReverse="True"
                                Duration="0:0:0.5"
                                FillBehavior="Stop"
                                Storyboard.TargetName="scrollButton"
                                Storyboard.TargetProperty="Opacity"
                                To="0.6" />
                            -->


                            <ColorAnimation AccelerationRatio="0.6"
                                            AutoReverse="True"
                                            Duration="0:0:0.5"
                                            FillBehavior="Stop"
                                            Storyboard.TargetName="scrollButton"
                                            Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)"
                                            To="#AAFF500E" />


                            <!--
                                <DoubleAnimation AccelerationRatio="0.6"
                                AutoReverse="True"
                                Duration="0:0:1"
                                FillBehavior="Stop"
                                Storyboard.TargetName="scrollButton"
                                Storyboard.TargetProperty="(RenderTransform).(ScaleTransform.ScaleY)"
                                To="0.5" />
                                
                                <DoubleAnimation AccelerationRatio="0.6"
                                AutoReverse="True"
                                Duration="0:0:1"
                                FillBehavior="Stop"
                                Storyboard.TargetName="scrollButton"
                                Storyboard.TargetProperty="(RenderTransform).(RotateTransform.Angle)"
                                To="45" />
                            -->
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
        </DataTemplate.Triggers>
    </DataTemplate>

</ResourceDictionary>